#include "../header/common.h"
#include <time.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <fcntl.h>
#include <SDL/SDL_ttf.h>

#define MAXBALLS 1
#define MAXOBJECTS 6
#define BUF_SIZ 4096
#define MAXPOINTS 11

void special2(int activate,SDL_Rect obj[],SDL_Surface *barls,SDL_Surface *barrs,SDL_Surface *screen,int *killeractive,int *barspeed,int *maxballs)
{
  SDL_Rect clear;
  int forceeffect=0;
  if(screen->w==640)
    {
      switch(activate)
	{
	case 0 : /*Lösche Special*/
 	  *barspeed=15; 
	  *killeractive=0;
	  
	  /*Linker Balken*/
	  clear.x=obj[1].x;clear.y=obj[1].y;
	  clear.w=barls->w;clear.h=barls->h;
	  SDL_FillRect(screen,&clear,SDL_MapRGB(screen->format,255,255,255)); 
	  SDL_FreeSurface(barls);
	  barls = SDL_LoadBMP("image/640480/barl.bmp"); 
	  SDL_BlitSurface(barls,NULL,screen,&obj[1]);

	  /*Rechter Balken*/
	  clear.x=obj[0].x;clear.y=obj[0].y;
	  clear.w=barrs->w;clear.h=barrs->h;
	  SDL_FillRect(screen,&clear,SDL_MapRGB(screen->format,255,255,255)); 
	  SDL_FreeSurface(barrs);
	  barrs = SDL_LoadBMP("image/640480/barr.bmp");
	  SDL_BlitSurface(barrs,NULL,screen,&obj[0]);
	  break;    
	case 1 : /*Linker Balken*/ 
	  isomorph(obj[0],screen,barls,killeractive,barspeed,maxballs,&forceeffect);
	  break;
	case 2 : /*Rechter Balken*/
	  isomorph(obj[1],screen,barrs,killeractive,barspeed,maxballs,&forceeffect);
	  break;
	}
    }

}
void ipeingeben(SDL_Surface *screen, char name[50])
{
  SDL_Surface *back,*button[6],*strich, *temp,*fehler;
  SDL_Rect rect[5],curser;
  SDL_Event event;
  SDL_Color color={255,255,255}; 
  int i,buffer1[6],bufffer2[8],crux,A[100]={0},S[2]={0},T[4]={0},textfeld=0,muh=0,muh1=0,done=0,click,font_size;
  TTF_Font *font;
  SDL_MouseMotionEvent mm;
  SDL_MouseButtonEvent mb;
  char ip[20]={"0"};
  FILE *datei;
  char tmp[50]={"0"};
  char dateiname[20]="tmp-ip.txt";
  /* ttf wird inisalisiert */
  TTF_Init();

  /*Bilder und auflösungsabhängige Werte werden geladen */
  switch((screen->w))
    {
    case 640 :
      back=SDL_LoadBMP("image/640480/ipeingeben.bmp");

      button[0]=SDL_LoadBMP("image/640480/buttons/abbrechen1.bmp");
      button[3]=SDL_LoadBMP("image/640480/buttonspressed/abbrechen2.bmp");
      button[1]=SDL_LoadBMP("image/640480/buttons/weiter1.bmp");      
      button[4]=SDL_LoadBMP("image/640480/buttonspressed/weiter2.bmp");      
      button[2]=SDL_LoadBMP("image/640480/kreuz1.bmp");      
      button[5]=SDL_LoadBMP("image/640480/kreuz2.bmp");      
      strich=SDL_LoadBMP("image/640480/strich0.bmp");
      temp=SDL_LoadBMP("image/640480/temp.bmp");
      font_size=16;     
      
      rect[4].x=160;rect[4].y=120;
      rect[0].x=190;rect[0].y=255;rect[0].w=button[0]->w;rect[0].h=button[0]->h;
      rect[1].x=330;rect[1].y=255;rect[1].w=button[1]->w;rect[1].h=button[1]->h;
      rect[2].x=260;rect[2].y=205;rect[2].w=button[2]->w;rect[2].h=button[2]->h;      
      curser.x=240;curser.y=184;
      rect[3].x=240;rect[3].y=184;rect[3].w=142;rect[3].h=14;      
      break;
      
    case 800 :
      back=SDL_LoadBMP("image/800600/ipeingeben.bmp");
          
	button[0]=SDL_LoadBMP("image/800600/buttons/abbrechen1.bmp");
      button[3]=SDL_LoadBMP("image/800600/buttonspressed/abbrechen2.bmp");
      button[1]=SDL_LoadBMP("image/800600/buttons/weiter1.bmp");      
      button[4]=SDL_LoadBMP("image/800600/buttonspressed/weiter2.bmp");      
      button[2]=SDL_LoadBMP("image/800600/kreuz1.bmp");      
      button[5]=SDL_LoadBMP("image/800600/kreuz2.bmp");      
      strich=SDL_LoadBMP("image/800600/strich0.bmp");
      temp=SDL_LoadBMP("image/800600/temp.bmp");
      font_size=18;     
      
      rect[4].x=200;rect[4].y=150;
      rect[0].x=238;rect[0].y=319;rect[0].w=button[0]->w;rect[0].h=button[0]->h;
      rect[1].x=413;rect[1].y=319;rect[1].w=button[1]->w;rect[1].h=button[1]->h;
      rect[2].x=330;rect[2].y=262;rect[2].w=button[2]->w;rect[2].h=button[2]->h;      
      curser.x=300;curser.y=230;
      rect[3].x=300;rect[3].y=230;rect[3].w=178;rect[3].h=16; 
      break;
      
    case 1024 :
      back=SDL_LoadBMP("image/1024768/ipeingeben.bmp");
      
      button[0]=SDL_LoadBMP("image/1024768/buttons/abbrechen1.bmp");
      button[3]=SDL_LoadBMP("image/1024768/buttonspressed/abbrechen2.bmp");
      button[1]=SDL_LoadBMP("image/1024768/buttons/weiter1.bmp");      
      button[4]=SDL_LoadBMP("image/1024768/buttonspressed/weiter2.bmp");      
      button[2]=SDL_LoadBMP("image/1024768/kreuz1.bmp");      
      button[5]=SDL_LoadBMP("image/1024768/kreuz2.bmp");      
      strich=SDL_LoadBMP("image/1024768/strich0.bmp");
      temp=SDL_LoadBMP("image/1024768/temp.bmp");
      font_size=20;     
      
      rect[4].x=256;rect[4].y=192;
      rect[0].x=305;rect[0].y=408;rect[0].w=button[0]->w;rect[0].h=button[0]->h;
      rect[1].x=529;rect[1].y=408;rect[1].w=button[1]->w;rect[1].h=button[1]->h;
      rect[2].x=416;rect[2].y=335;rect[2].w=button[2]->w;rect[2].h=button[2]->h;      
      curser.x=384;curser.y=298;
      rect[3].x=384;rect[3].y=296;rect[3].w=228;rect[3].h=18; 
      break;
    }
  

  /* Fenster wird gezichent */
 font=TTF_OpenFont("arial.ttf",font_size-2);
 

  A[0]=rect[3].x;
  S[0]=rect[3].x;
  T[0]=rect[3].x;
  T[1]=rect[3].y;
  T[2]=rect[3].w;
  T[3]=font_size;
  
  SDL_SetColorKey(back, SDL_SRCCOLORKEY, SDL_MapRGB(back->format, 0, 0, 255));
  SDL_BlitSurface(back,NULL,screen,&rect[4]);
  SDL_BlitSurface(button[0],NULL,screen,&rect[0]);
  SDL_BlitSurface(button[1],NULL,screen,&rect[1]);
  SDL_BlitSurface(button[2],NULL,screen,&rect[2]);
  SDL_FillRect(screen,&rect[3],SDL_MapRGB(screen->format,0,0,0));
  SDL_BlitSurface(strich,NULL,screen,&curser);
  SDL_Flip(screen);
  
  
  /*Datai wird ausgelseen , obip gespeichert wurde */ 
    datei = fopen (dateiname, "r");
  fgets(tmp, 50, datei);
  fclose(datei);
  crux=tmp[0]-48;
  
  /* Falls ja, wird sie hier gezeichnet*/
  if(crux)
    {
      namenzeichen(screen,A,S,T,tmp,ip,font);    
      SDL_BlitSurface(button[5],NULL,screen,&rect[2]);
    }
  

  muh=SDL_EnableKeyRepeat(500,20);
  SDL_EnableUNICODE(1); 
  
  /* Eventschleife wird gestartet*/
while(!done)
  {
    if(textfeld)
      { /* curser blinken*/
	if(muh1>4)
	  {
	    SDL_BlitSurface(strich,NULL,screen,&curser);
	    muh++;
	  }
	else muh1++;
	if(muh>4)
	  {
	    SDL_FillRect(screen,&curser,SDL_MapRGB(screen->format,0,0,0));
	    muh1=0;
	    muh=0;
	  }
      }
    while (SDL_PollEvent(&event))
      {
	switch(event.type)
	  {
	  case SDL_QUIT:
	    done = 1;
	    break;
	    
	    /* tastendruck wird ausgewertet*/
	  case SDL_KEYDOWN:
	    if(textfeld)
	      { 
		if((event.key.keysym.unicode>=32)&&(event.key.keysym.unicode<=126)|(event.key.keysym.unicode>=160)&&(event.key.keysym.unicode<=255))
		  { 
		    bschreiben2(screen,event.key.keysym.unicode,font,ip,A,S,0,255,0,T);
		    SDL_FillRect(screen,&curser,SDL_MapRGB(screen->format,0,0,0));
		    curser.x=S[0];
		  }
	      }
	    switch(event.key.keysym.sym)
	      {
	      case SDLK_ESCAPE:
		done=1;
		break;
		/* löschen*/
	      case SDLK_BACKSPACE:
		if((S[1]!=0)&&(textfeld))
		  {
		    loeschen(screen,font,ip, A, S,0,T);
		    curser.x=S[0];
		  }		 
		break;
		
	      case SDLK_DELETE:
		if((S[1]<A[1])&&(textfeld)) 
		  {
		    loeschen(screen,font,ip, A, S,1,T);
		    curser.x=S[0];
		  }	 
		break;
		/*cursor bewegen*/
	      case SDLK_LEFT:
		if((S[1]>0)&&(textfeld))
		  { 
		    curserbewegen(ip,A,S,-1,T);
		    SDL_FillRect(screen,&curser,SDL_MapRGB(screen->format,0,0,0));
		    curser.x=S[0];
		  }
		break;
	      case SDLK_RIGHT:
		if((S[1]<A[1])&&(textfeld))
		  {
		    curserbewegen(ip,A,S,1,T);
		    SDL_FillRect(screen,&curser,SDL_MapRGB(screen->format,0,0,0));
		    curser.x=S[0];
		  }
		break;
	      }
	    /* mousebewegen für mouseoverbutton*/
	  case SDL_MOUSEMOTION:
	    mm=event.motion;	  
	    for(i=0;i<2;i++)
	      {
		click=click_button(rect[i],mm.x,mm.y);
		switch(click)
		  {
		  case 0:
		    SDL_BlitSurface(button[i],NULL,screen,&rect[i]);
		    break;
		  case 1:
		    SDL_BlitSurface(button[i+3],NULL,screen,&rect[i]);
		    break;
		  }
	      }
	    break;  
	    /*mouseclick wird ausgewertet*/
	  case SDL_MOUSEBUTTONDOWN:
	    mb=event.button;
	    for(i=0;i<4;i++)
	      {
		click=click_button(rect[i],mb.x,mb.y);
		if(click)
		  {
		    switch(i)
		      {
		      case 0:
			done=1;
			break;
		      case 1:
			if(crux) data2(ip,dateiname);		      
			client(screen,ip,name);
			done=1;		      
			break;
		      
		      case 2:
			if(!crux)
			  {/*haken wird bei ip speichern gesetzt und ind atei geschrieben */
			    
			    SDL_BlitSurface(button[5],NULL,screen,&rect[2]);
			    crux=1;data(1,dateiname);
			  }
			else 
			  {/*habken wird wieder rausgenommen und wiede rin datei geschrieben */ 
			    SDL_BlitSurface(button[2],NULL,screen,&rect[2]);
			    crux=0;data(0,dateiname);
			  }
			break;
			/*es wird ins Textfeld geklickt,aktiviert das textfeld*/
		      case 3:
			textfeld=1;
			curserclick(ip,A,S,T,mb.x);
			SDL_FillRect(screen,&curser,SDL_MapRGB(screen->format,0,0,0));
			curser.x=S[0];
		      }
		  }  
		else
		  {
		    textfeld=0;
		    SDL_BlitSurface(strich,NULL,screen,&curser);
		  }
	      }break;
	  }
      }
    SDL_Flip(screen);
    SDL_Delay(100);
  }
/*bilder werden freigegeben*/
 SDL_FreeSurface(back);
 SDL_FreeSurface(button[0]);
 SDL_FreeSurface(button[1]);
 SDL_FreeSurface(button[2]);
 SDL_FreeSurface(button[3]);
 SDL_FreeSurface(button[4]);
 SDL_FreeSurface(button[5]);
 SDL_FreeSurface(strich);
SDL_FreeSurface(temp);
 
}


int client(SDL_Surface *screen, char ip[20],char name[50])
{
  SDL_Surface *balls, *barrs, *barls, *background, *boxs,*kreis,*warten,*button1,*button2,*fehler;
  SDL_Rect obj[MAXOBJECTS],ball[2],field,box[1],kreis1,kreis2,rect1,rect2,rect3,rect;
  SDL_Event event;
  int done=0,ballnumber,pointr=0,pointl=0,wait=0,sound,tmp,activebox=0,activeboxtmp=1,steptmp;
  int xway[2],yway[2],frames=0,step,level=10,autoaim=0,goboxtime=10,killeractive=0,maxballs=1;
  int infoplace,infozeile=2,font3high,autoaimtmp,go,xwaytmp=0,kill=0,ywaytmp=0,barspeed,points;
  int buffer1[4],buffer2[21],done2=0,s,click=0, bytes,i,muh=0,funzt,go1=0,go2=0,mode =3,forceeffect,sbox,loeschen=0,x=0;
  long unsigned int seconds=1,boxtime=time(NULL),pointtime;
  TTF_Font *font, *font2, *font3,*font4;
  char string[10],stringlong[35],pointstring[20];
  socklen_t addr_len;  
 struct sockaddr_in addr;
   char name2[50];
  SDL_MouseMotionEvent mm;
  SDL_MouseButtonEvent mb;


  TTF_Init();

  srand (time(NULL));

  /*Balkenpositionen werden festgelegt*/
  obj[1].x=50;obj[1].y=150;
  obj[0].x=((screen->w)-(screen->w/10));obj[0].y=150;
  field.x=(screen->w)/33;field.w=(screen->w)-(field.x*2);
  field.y=(screen->h)/5-((screen->w)/100);field.h=(screen->h)-((screen->h)/4)-((screen->w)/25);
  switch((screen->w))
    {
    case 640 :
      font = TTF_OpenFont("spranq_eco_sans_regular.ttf", 50);
      font2 =TTF_OpenFont("arial.ttf", 20);
      font3 = TTF_OpenFont("arial.ttf", 10);
      font4=TTF_OpenFont("arial.ttf", 16);      
      balls = SDL_LoadBMP("image/640480/ball.bmp");
      barrs = SDL_LoadBMP("image/640480/barr.bmp");
      barls = SDL_LoadBMP("image/640480/barl.bmp");
      background = SDL_LoadBMP("image/640480/background.bmp");
      boxs = SDL_LoadBMP("image/640480/box.bmp");
      step=5;
      font3high=15;
      barspeed=15; 
      rect.x=160;rect.y=120;
      fehler=SDL_LoadBMP("image/640480/fehler.bmp");
      break;

    case 800 :
      font = TTF_OpenFont("spranq_eco_sans_regular.ttf", 70);
      font2=TTF_OpenFont("arial.ttf", 30); 
      font3 = TTF_OpenFont("arial.ttf", 13);  
      font4=TTF_OpenFont("arial.ttf", 16);        
      balls = SDL_LoadBMP("image/800600/ball.bmp");
      barrs = SDL_LoadBMP("image/800600/barr.bmp");
      barls = SDL_LoadBMP("image/800600/barl.bmp");
      background = SDL_LoadBMP("image/800600/background.bmp");
      boxs = SDL_LoadBMP("image/800600/box.bmp");
      fehler=SDL_LoadBMP("image/800600/fehler.bmp");      
      step=7;
      rect.x=200;rect.y=150;
      font3high=20;
      barspeed=23;
      break;

    case 1024 :
      font = TTF_OpenFont("spranq_eco_sans_regular.ttf", 90);
      font2=TTF_OpenFont("arial.ttf", 40);
      font3 = TTF_OpenFont("arial.ttf", 16);  
      font4=TTF_OpenFont("arial.ttf", 16);   
      balls = SDL_LoadBMP("image/1024768/ball.bmp");
      barrs = SDL_LoadBMP("image/1024768/barr.bmp");
      barls = SDL_LoadBMP("image/1024768/barl.bmp");
      background = SDL_LoadBMP("image/1024768/background.bmp");
      boxs = SDL_LoadBMP("image/1024768/box.bmp");
      step=9;
      rect.x=256;rect.y=192;
      font3high=30;
      barspeed=30;
      fehler=SDL_LoadBMP("image/1024768/fehler.bmp");
      break;
    }
  
  /*socket wird gestartet*/
  s = socket(PF_INET, SOCK_STREAM, 0);
  if (s == -1)
    {
      perror("socket() failed");
      
    }
  
  
  addr.sin_addr.s_addr = inet_addr(ip); 
  addr.sin_port=htons(8080); 
  addr.sin_family = AF_INET; 
  /* wird probiert zu connecten*/ 
  if (connect(s, (struct sockaddr*)&addr, sizeof(addr)) == -1)
    {
      perror("connect() failed");
     SDL_SetColorKey(fehler, SDL_SRCCOLORKEY, SDL_MapRGB(fehler->format, 0, 0, 255));
      SDL_BlitSurface(fehler,NULL,screen,&rect);
      SDL_Flip(screen);
      SDL_Delay(5000);
      SDL_FreeSurface(background);
      SDL_FreeSurface(fehler);
      SDL_FreeSurface(barls);
      SDL_FreeSurface(barrs);
      SDL_FreeSurface(balls);
      SDL_FreeSurface(boxs);      
      return 0;

    }
  /*fcntl(s, F_SETFL, O_NONBLOCK);*/




  

  SDL_BlitSurface(background,NULL,screen,NULL);
  SDL_FillRect(screen,&field,SDL_MapRGB(screen->format,255,255,255));  
 
    
  steptmp=step;

  /*Box wird initialisiert*/
  box[0].w=boxs->w;box[0].h=boxs->h;
  box[0].x=(obj[0].x+(barls->w));
  box[0].y=((int)(field.y+field.h-box[0].h)*(rand()/(RAND_MAX+1.0)));
  if(box[0].y<field.y)box[0].y+=field.y;
  
 /* NAMEN WERDEN AUSGETAUSCHT UND GEBLITTET*/
  send(s,name,(strlen(name)),0);
  bytes=recv(s,name2,(sizeof(name2)),0);
  name2[bytes]=0;
  
  
  SDL_BlitSurface(background,NULL,screen,NULL);
  SDL_FillRect(screen,&field,SDL_MapRGB(screen->format,255,255,255));  
  drawText(screen,screen,font2,name2,((screen->w)/12),(screen->h/15)*14,0,0,0);
  drawText(screen,screen,font2,name,((screen->w)/15)*11,(screen->h/15)*14,0,0,0);

  for(ballnumber=0;ballnumber<2;ballnumber++)
    {
      /*Ballgeschwindigkeiten werden geladen*/
      xway[ballnumber]=1+(2*-ballnumber);yway[ballnumber]=1+(2*-ballnumber);
      /*Ballgroesse wird uebergeben*/
      ball[ballnumber].w = balls->w;
      ball[ballnumber].h = balls->h;
      /*Ballstartpositionen werden vergeben*/
      ball[ballnumber].x=(((screen->w)/2)+(balls->w*ballnumber*3));
      ball[ballnumber].y=(((screen->h)/2)-(balls->h*ballnumber*3));
    }

  /*Balken werden gezeichnet*/
  
  infoplace=((screen->w/10));infozeile=0;
  drawText(screen,background,font3,"-------Info-------",infoplace,(infozeile*font3high+2),255,255,255);
  
  options(1,&sound,&tmp);
  if(sound==1)play(1);

  snprintf(string,sizeof(string),"%d",pointr);
  drawText(screen,background,font,string,(screen->w/100)*38 ,(screen->h/35),255,255,255);
  snprintf(string,sizeof(string),"%d",pointl);
  drawText(screen,background,font,string,(screen->w/100)*63,(screen->h/35),255,255,255);
  string[0]=0; 

  drawText(screen,background,font,"Ready!",((screen->w/2)-(screen->w/10)),((screen->h/2)-(screen->w/10)),255,0,0);
  SDL_Flip(screen);
  SDL_Delay(1000);
  drawText(screen,background,font,"Steady!",((screen->w/2)-(screen->w/10)),((screen->h/2)-(screen->w/10)),255,0,0);
  SDL_Flip(screen);
  SDL_Delay(1000);
  drawText(screen,background,font,"GO!!!      ",((screen->w/2)-(screen->w/10)),((screen->h/2)-(screen->w/10)),255,0,0);
  SDL_Flip(screen);
  SDL_Delay(500);
  drawText(screen,background,font,"                ",((screen->w/2)-(screen->w/10)),((screen->h/2)-(screen->w/10)),255,0,0);

  pointtime=time(NULL);
/*Programm Exit wird festgelegt*/    
  while ((!done)&&(!done2))
    {
      while (SDL_PollEvent(&event)) 
	{
	  switch(event.type) 
	    {
	    case SDL_QUIT:
	      done=1;
	      break;
	    }
	}
      /*Balken werde geloescht*/
      
obj[0].w=barls->w;
obj[0].h=barls->h;
      /*Tastatur wird gecheckt*/
     SDL_FillRect(screen,&obj[0],SDL_MapRGB(screen->format,255,255,255));
      go1=keyhandler(obj,screen,&done,&mode,&autoaim,&barspeed);
      /* balkendaten werden zwischen client und server ausgetauscht*/ 
      /*send(s,&obj[0],(sizeof(SDL_Rect)),0);
      bytes=recv(s,&obj[1],(sizeof(SDL_Rect)),0);*/
      
      SDL_BlitSurface(barrs,NULL,screen,&obj[0]);
      

     	   

      /*Ballberechnung wird initialisiert*/
      for(ballnumber=0;ballnumber<maxballs;ballnumber++)
	{
	  if(ball[ballnumber].x<(obj[1].x-10))
	    {SDL_FillRect(screen,&ball[ballnumber],SDL_MapRGB(screen->format,255,255,255));
	      burn(ball[ballnumber],screen);
	      pointr++;
	      snprintf(string,sizeof(string),"%d",pointr);
	      drawText(screen,background,font,string,(screen->w/100)*63,(screen->h/35),255,255,255);	      
	      wait=1;
	      ball[ballnumber].x=(((screen->w)/2)+((ballnumber*3)*(balls->w)));
	      ball[ballnumber].y=(((screen->h)/2)+((ballnumber*3)*(balls->h)));
	    }
	  if(ball[ballnumber].x>(obj[0].x+10))
	    {SDL_FillRect(screen,&ball[ballnumber],SDL_MapRGB(screen->format,255,255,255));
	      burn(ball[ballnumber],screen);
	      pointl++;	       
	      snprintf(string,sizeof(string),"%d",pointl);
	      drawText(screen,background,font,string,(screen->w/100)*38,(screen->h/35),255,255,255);
	      wait=1;
	      ball[ballnumber].x=(((screen->w)/2)+((ballnumber*3)*(balls->w)));
	      ball[ballnumber].y=(((screen->h)/2)+((ballnumber*3)*(balls->h)));
	    }	  
 	  if((pointl>MAXPOINTS)||(pointr>MAXPOINTS)){done=1;wait=0;} 
	  
	}

      /*Bilder pro Sekunde werden berechnet und ausgegeben*/
      if(seconds-time(NULL)>=1)
	{
	  seconds=time(NULL);
	  snprintf(string,sizeof(string),"%d FPS",frames);infozeile=1;
	  drawText(screen,background,font3,string,infoplace,(infozeile*font3high+2),255,255,255);
	  frames=0;

	  if(kill>(screen->w/100))
	    {
 	      wait=1;
	      SDL_FillRect(screen,&ball[0],SDL_MapRGB(screen->format,255,255,255));
	      SDL_FillRect(screen,&ball[1],SDL_MapRGB(screen->format,255,255,255));
	    }
	   kill=0;
	}   

      /*Das Spiel wartet 15ms um ca. 60FPS zu erreichen*/
      SDL_Delay(10);  
if(activebox)
  {SDL_FillRect(screen,&box[0],SDL_MapRGB(screen->format,255,255,255));}
for(ballnumber=0;ballnumber<maxballs;ballnumber++)
   {      
     SDL_FillRect(screen,&ball[ballnumber],SDL_MapRGB(screen->format,255,255,255));
   }
    x++;  
/* austausch zwischen client und server */
obj[1].w=barrs->w;
obj[1].h=barrs->h;
      SDL_FillRect(screen,&obj[1],SDL_MapRGB(screen->format,255,255,255));     
 if(x==3)
{
	buffer1[0]=done;
      buffer1[1]=go1;
buffer1[2]=obj[0].x;  
buffer1[3]=obj[0].y;  

      
     
      /*SDL_FillRect(screen,&ball[0],SDL_MapRGB(screen->format,255,255,255));*/
      send(s,buffer1,4*(sizeof(int)),0);
      bytes=recv(s,buffer2,21*(sizeof(int)),0);
      if(bytes >0)
	{
	 
	  ball[0].x=buffer2[0];
	  ball[0].y=buffer2[1];  
	  ball[0].w=buffer2[2]; 
	  ball[0].h=buffer2[3];
	  done2=buffer2[4];
	  go2=buffer2[5];
	  sbox=buffer2[6];
	  forceeffect=buffer2[7];
	  maxballs=buffer2[8];
	  ball[1].x=buffer2[9];
	  ball[1].y=buffer2[10];
	  ball[1].w=buffer2[11];
	  ball[1].h=buffer2[12];
	  box[0].x=buffer2[13];
	  box[0].y=buffer2[14];
	  box[0].w=buffer2[15];
	  box[0].h=buffer2[16];
	  activebox=buffer2[17];
	  goboxtime=buffer2[18];
		obj[1].x=buffer2[19];
obj[1].y=buffer2[20];


	} 
x=0;} 

  
  
SDL_BlitSurface(barls,NULL,screen,&obj[1]);
 

    /*reagaiert auf das autreten von boxen*/
      if(activebox)
	{
	  SDL_BlitSurface(boxs,NULL,screen,&box[0]);
	  if(!loeschen)
	    {
	      special2(0,obj,barls,barrs,screen,&killeractive,&barspeed,&maxballs);loeschen=1;
	     infozeile=2;
	      snprintf(stringlong,sizeof(stringlong),"Next box in %d s  ",goboxtime);
	      drawText(screen,background,font3,stringlong,infoplace,(infozeile*font3high+2),255,255,255);
	    }
	}
      else
	{if(loeschen)
	  {loeschen=0;}
	}
      /*zeichnet bälle*/
 for(ballnumber=0;ballnumber<maxballs;ballnumber++)
   {      
    SDL_BlitSurface(balls,NULL,screen,&ball[ballnumber]);
   }
     

 /* box evente werden erzeugt*/
 if(sbox==1)
   {
 
     isomorph(obj[1],screen,barls,&killeractive,&barspeed,&maxballs,&forceeffect);
     sbox=0;
     SDL_FillRect(screen,&box[0],SDL_MapRGB(screen->format,255,255,255));
     SDL_BlitSurface(barls,NULL,screen,&obj[0]);
     box[0].x=(obj[0].x+obj[0].w);
     box[0].y=((int)(field.y+field.h-box[0].h)*(rand()/(RAND_MAX+1.0)));
     if(box[0].y<field.y)box[0].y+=field.y;
   }
 if(sbox==2)
   {
     
     isomorph(obj[0],screen,barrs,&killeractive,&barspeed,&maxballs,&forceeffect);
     sbox=0;
     SDL_FillRect(screen,&box[0],SDL_MapRGB(screen->format,255,255,255));
     SDL_BlitSurface(barrs,NULL,screen,&obj[1]);
     box[0].x=(obj[1].x-box[0].w);
     box[0].y=((int)(field.y+field.h-box[0].h)*(rand()/(RAND_MAX+1.0)));
     if(box[0].y<field.y)box[0].y+=field.y;
   }
 
 /* chat wird gestartet*/
 if((go2==-1)|(go1==-1))
	{
	  SDL_Delay(100);
	  chat(screen,s,name);
	  fcntl(s,F_SETFL,!O_NONBLOCK);
	  SDL_Delay(400);
	}  

 /*Die Bildschirm-Surface wird "gewendet"*/
      SDL_Flip(screen);

      if((go1)|(go2))
	{ drawText(screen,background,font,"GO!!!",((screen->w/2)-(screen->w/10)),((screen->h/2)-(screen->w/10)),255,0,0);
	  SDL_Flip(screen);
	  SDL_Delay(300);
	  drawText(screen,background,font,"          ",((screen->w/2)-(screen->w/10)),((screen->h/2)-(screen->w/10)),255,0,0);
	  step=steptmp;
	  wait=0;
	  maxballs=1;
	}
      /*Frames werden hochgezaehlt*/
      frames++;
    }

  /*Die Surfaces werden wieder geloescht*/
 SDL_FreeSurface(balls);
  SDL_FreeSurface(barls);
  SDL_FreeSurface(barrs); 
    SDL_FreeSurface(boxs);
  SDL_FreeSurface(fehler); 
  
    
  /*play(0);*/
  

  /*Darstellung des Gewinners*/
  if((pointl>MAXPOINTS)||(pointr>MAXPOINTS))
    {
      /*Berechnung der Punkte*/
      if(mode==1)
	{
	  points=((600-(time(NULL)-pointtime)-pointr)*(level+1));
	  snprintf(pointstring,sizeof(pointstring),"with %d points!!!",points);
	}
      drawText(screen,background,font,"And the winner is...",80,((screen->h/2)-((screen->w/10)*2)),255,0,0);
      SDL_Flip(screen);
      SDL_Delay(1000);
      if(pointl<pointr)
	{
	  if(mode==1)drawText(screen,background,font,"Mr. Computer!",80,((screen->h/2)-(screen->w/10)),255,0,0);
	  if(mode==2)drawText(screen,background,font,"The RIGHT player!",80,((screen->h/2)-(screen->w/10)),255,0,0);
	}
      else
	{
	  if(mode==1)
	    {
	      drawText(screen,background,font,name,screen->w/3,((screen->h/2)-(screen->w/10)),255,0,0);
	      drawText(screen,background,font,pointstring,screen->w/6,((screen->h/3)*2),255,0,0);
	    }
	  if(mode==2)drawText(screen,background,font,"The LEFT player!",80,((screen->h/2)-(screen->w/10)),255,0,0);
	}
      SDL_Flip(screen);
 
      SDL_Delay(3000);
    }
  /*background wird freigegeben und socket geschlossen */
  SDL_FreeSurface(background);
  close(s);
 
return 0;

 
}






